package com.company.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.exception.BusinessException;
import com.company.project.common.exception.code.BaseResponseCode;
import com.company.project.entity.SysDept;
import com.company.project.mapper.SysDeptMapper;
import com.company.project.vo.resp.ClassRespNodeVO;
import com.company.project.vo.resp.DeptRespNodeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.company.project.mapper.SysClassificationMapper;
import com.company.project.entity.SysClassificationEntity;
import com.company.project.service.SysClassificationService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service("sysClassificationService")
public class SysClassificationServiceImpl extends ServiceImpl<SysClassificationMapper, SysClassificationEntity> implements SysClassificationService {

    @Resource
    private SysClassificationMapper sysClassificationMapper;
    @Override
    public List<ClassRespNodeVO> classTreeList(String classId, Boolean disabled) {
        List<SysClassificationEntity> list;
        if (StringUtils.isEmpty(classId)) {
            list = sysClassificationMapper.selectList(Wrappers.emptyWrapper());
        } else {
            SysClassificationEntity sysClass = sysClassificationMapper.selectById(classId);
            if (sysClass == null) {
                throw new BusinessException(BaseResponseCode.DATA_ERROR);
            }
            LambdaQueryWrapper<SysClassificationEntity> queryWrapper = Wrappers.lambdaQuery();
            List<Object> childIds = sysClassificationMapper.selectObjs(queryWrapper);
            list = sysClassificationMapper.selectList(Wrappers.<SysClassificationEntity>lambdaQuery().notIn(SysClassificationEntity::getId, childIds));
        }
        // 默认加一个顶级方便新增顶级分类
        ClassRespNodeVO respNodeVO = new ClassRespNodeVO();
        respNodeVO.setTitle("默认顶级分类");
        respNodeVO.setId("0");
        respNodeVO.setSpread(true);
        respNodeVO.setDisabled(disabled);
        respNodeVO.setChildren(getTree(list));
        List<ClassRespNodeVO> result = new ArrayList<>();
        result.add(respNodeVO);
        return result;
    }

    private List<ClassRespNodeVO> getTree(List<SysClassificationEntity> all) {
        List<ClassRespNodeVO> list = new ArrayList<>();
        for (SysClassificationEntity sysClass : all) {
            if ("0".equals(sysClass.getPId())) {
                ClassRespNodeVO classTree = new ClassRespNodeVO();
                BeanUtils.copyProperties(sysClass, classTree);
                classTree.setTitle(subTitle(sysClass.getName()));
                classTree.setRealTitle(sysClass.getName());
                classTree.setSpread(false);
                classTree.setChildren(getChild(classTree.getId(), all));
                list.add(classTree);
            }
        }
        return list;
    }

    private List<ClassRespNodeVO> getChild(String id, List<SysClassificationEntity> all) {
        List<ClassRespNodeVO> list = new ArrayList<>();
        for (SysClassificationEntity sysClass : all) {
            if (sysClass.getPId().equals(id)) {
                ClassRespNodeVO classTree = new ClassRespNodeVO();
                BeanUtils.copyProperties(sysClass, classTree);
                classTree.setTitle(subTitle(sysClass.getName()));
                classTree.setRealTitle(sysClass.getName());
                classTree.setChildren(getChild(classTree.getId(), all));
                list.add(classTree);
            }
        }
        return list;
    }

    private String subTitle(String orgStr){
        return "<span title=\""+orgStr+"\">"+(orgStr.length() > 15 ? orgStr.substring(0,12) + "...":orgStr)+"</span>";
    }

    @Override
    public List<Object> selectObjs(LambdaQueryWrapper<SysClassificationEntity> wrapper) {
        return sysClassificationMapper.selectObjs(wrapper);
    }
}